import xml.parsers.expat
from JavaModelCodeGenerator import ModelCodeGen
from JavaDMCodeGenerator import DMCodeGen

class FileGen:
    def __init__(self):
        
        self.parser = xml.parsers.expat.ParserCreate()
        self.parser.StartElementHandler = self.start
        self.parser.EndElementHandler = self.end
        self.parser.CharacterDataHandler = self.data
        self.primaryKeyList = []
        self.columns = {}
        
    def start(self,name,attrs):

        if (name =="column"):
            print "name:",
            name =  attrs["name"]
            print name
            print "type:",
            type = attrs["type"]
            print type
           
            if (attrs.has_key("primaryKey")):
                print "primaryKey:",
                print attrs["primaryKey"]
                self.primaryKeyList.append(name)     
            self.columns[name] = type   
        elif (name=="table"):
            self.tableName = attrs["name"]
        elif (name=="modelClass"):
            self.modelClassName = attrs["name"]
    
    def end(self,name):
        pass
    
    def data(self,data):
        pass
    
    def feed(self,data):
        
        self.parser.Parse(data,0)
        
    def work(self,data):
        
        self.feed(data)
        print self.primaryKeyList
        print self.columns
        
        codeGen = ModelCodeGen(self.modelClassName,self.primaryKeyList,self.columns)
        codeGen.generate()
        
        codeGen = DMCodeGen(self.modelClassName,self.primaryKeyList,self.columns)
        codeGen.generate()
    
if(__name__=='__main__'):
    print 'd1---------->:'
    engine = FileGen()
    file_object = open('dataDef.xml','r')
    try:
        data = file_object.read()
        engine.work(data)
       
    finally:
        file_object.close()    
